package kr.hkit.powerjava.chapter26.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import kr.hkit.powerjava.chapter26.common.DbUtil;
import kr.hkit.powerjava.chapter26.dto.Employee;

public class EmployeeDAO implements DaoInterface<Employee> {

	private Connection connection;
	
	public EmployeeDAO(Connection connection) {
		this.connection = connection;
	}

	@Override //입력
	public void insertItem(Employee item) throws SQLException {
		String sql = "insert into employee values (?,?,?,?,?,?)";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.setString(2, item.getEmpName());
		pstmt.setString(3, item.getTitle());
		pstmt.setInt(4, item.getManager());
		pstmt.setInt(5, item.getSalary());
		pstmt.setInt(6, item.getDno());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
	}

	@Override //삭제
	public void deleteItemByNo(Employee item) throws SQLException {
		String sql = "delete from employee where empno=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
	}

	@Override //수정
	public void updateItemByNo(Employee item) throws SQLException {
		String sql = "update employee set title=?, manager=?, salary=?, dno=? where empno=? and empname =?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, item.getTitle());
		pstmt.setInt(2, item.getManager());
		pstmt.setInt(3, item.getSalary());
		pstmt.setInt(4, item.getDno());
		pstmt.setInt(5, item.getEmpNo());
		pstmt.setString(6, item.getEmpName());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
	}

	@Override //검색
	public Employee searchItem(Employee item) throws SQLException {
		Employee employee = null;
		String sql = "select empno,empname,title,manager,salary,dno from employee where empno=? and empname =?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.setString(2, item.getEmpName());
		ResultSet rs = pstmt.executeQuery();
		
		if (rs.next()) { 
			employee = new Employee();
			employee.setEmpNo(rs.getInt(1));
			employee.setEmpName(rs.getString(2));
			employee.setTitle(rs.getString(3));
			employee.setManager(rs.getInt(4));
			employee.setSalary(rs.getInt(5));
			employee.setDno(rs.getInt(6));
		} //있으면 employee생성. 없으면 null값 반환
		
		DbUtil.close(rs);
		DbUtil.close(pstmt);
		return employee;
	}
	
	@Override
	public ArrayList<Employee> listItems() throws SQLException {
		
		ArrayList<Employee> list = new ArrayList<>(); 
		String sql = "select empno,empname,title,manager,salary,dno from employee";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		
		while (rs.next()) { 
			list.add(new Employee(rs.getInt(1),rs.getString(2),
					rs.getString(3),rs.getInt(4),
					rs.getInt(5),rs.getInt(6)));
		} 
		
		DbUtil.close(rs);
		DbUtil.close(pstmt);
		return list;
	}
}
